//This is fairly rewritten, but as about 80% of the code is from Querystring, the licence remains

/* Client-side access to querystring name=value pairs
	Version 1.3
	28 May 2008
	
	License (Simplified BSD):
	http://adamv.com/dev/javascript/qslicense.txt
*/


/**
 * This code manipulates the hash string as if it was the same as a query string
 *
 * It is used in storing things like the current tab, etc
 */
require.def('lib/hashstring', [], function() {
	var params = {};

    var h = window.location.hash;	
    var qs = h.substring(1, h.length);

	if (qs.length !== 0){
        var args = qs.replace(/\+/g, ' ').split('&');
        
        for (var i = 0; i < args.length; i++) {
            var pair = args[i].split('=');
            var name = decodeURIComponent(pair[0]);
            
            var value = (pair.length==2)
                ? decodeURIComponent(pair[1])
                : name;
            
            params[name] = value;
        }
    }
    return {
        get: function(key, default_) {
            var value = params[key];
            return (value != null) ? value : default_;
        },
        contains: function(key) {
            var value = params[key];
            return (value != null);
        },
        set: function(key, value){
            params[key] = value;

            nvp = []; 
            for (var k in params ) {
                var v = params[k];
                var k = encodeURIComponent(k);
                
                var v = (v.length)
                    ? encodeURIComponent(v)
                    : v;

                nvp.push([k, v].join('='));
            }
            window.location.hash = nvp.join('&').replace(/ /g, '+');
        }

    }
});

